<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="pyjm" label="" class="w-1/5">
          <a-input v-model:value="formState.pyjm" placeholder="药品编号/药品名称/助记码" />
        </a-form-item>
        <a-form-item name="dateRange" label="日期范围">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="sfph" class="ml-5">
          <a-radio-group v-model:value="formState.sfph">
            <a-radio :value="0">不显示批号</a-radio>
            <a-radio :value="1">显示批号</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-3">
        <BaseExport
          type="门诊"
          name="西成药发药汇总"
          :code="formState.sfph ? '00001148' : '00001147'"
          :search-params="{
            stdate: formState['dateRange']?.[0],
            eddate: formState['dateRange']?.[1],
            operationmode: formState.operationmode,
            pyjm: formState.pyjm,
            sfph: formState.sfph,
            xmlbs: [2, 4]
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable :loading="loading" :column-code="formState.sfph ? '00001148' : '00001147'" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 明细 </a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.jjhz) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.sjhz) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" :form-data="formState" />
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import SearchContainer from '@/components/SearchContainer'
import { formatAmount } from '@/utils/format.js'
import Edit from './Edit.vue'

const editOpen = ref(false)
const formRef = ref(null)
const rowData = ref({})
const total = ref({})
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  operationmode: 4,
  sfph: 0
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzyf/getKcRolloverSummary',
  params: formState.value,
  immediate: true,
  formatParams: (params) => {
    return {
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1],
      operationmode: params.operationmode,
      pyjm: params.pyjm,
      sfph: params.sfph,
      xmlbs: [2, 4]
    }
  },
  formatResult: (result) => {
    total.value = result.reduce(
      (sum, current) => {
        return {
          jjhz: sum.jjhz + (current.jjhz ? Number(current.jjhz) : 0),
          sjhz: sum.sjhz + (current.sjhz ? Number(current.sjhz) : 0)
        }
      },
      { jjhz: 0, sjhz: 0 }
    )
    return result
  }
})

const onHandleEdit = (row) => {
  editOpen.value = true
  rowData.value = row
}

watch(
  () => formState.value.sfph,
  () => {
    getData()
  }
)
</script>
